unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

const
A = 30;


type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  arr: array of TPanel;
  n, u, i, p, k, q: integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (u = 1) then
    for i:= 0 to n-1 do
    begin
      arr[i].Destroy;
    end;
  n:=StrToInt (Edit1.Text);
  setlength (arr, n);
  randomize;
  for i:= 0 to n-1 do
    begin
      arr[i]:= TPanel.Create(Form1);
      arr[i].Parent:= Form1;
      arr[i].Width := A;
      arr[i].Height := A;
      p:=1;
      k:=1;
      q:=0;
      while (2*p < (i+2)) do
      begin
        p:=p*2;
        k:=k+1;
        if (2*p = (i+1)) then
        q:=1;
      end;

      arr[i].Top := (A+10)*(k+1);
      if (i = 0) or (i=1) or (q = 1) then
        arr[i].Left := Form1.Width div (2*p)
      else
        arr[i].Left:= arr[i-1].Left + 2*Form1.Width div (2*p)+1 ;
      //arr[i].Caption := IntToStr(Random (50)+1);
      arr[i].Caption := IntToStr(i+1);
      arr[i].Color:=clTeal;
      arr[i].Font.Color:= clWhite;
    end;
  u:=1;
end;

end.
